import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';

import 'package:whale/helper/route/route_navigator.dart';

import '../../../../helper/utils/util.dart';

class ReadDealWidget extends StatefulWidget {
  const ReadDealWidget({
    Key? key,
    this.actionAnimate,
    required this.valueChanged,
  }) : super(key: key);
  final Function(AnimationController)? actionAnimate;
  final ValueChanged<bool> valueChanged;
  @override
  State<ReadDealWidget> createState() => _ReadDealWidgetState();
}

class _ReadDealWidgetState extends State<ReadDealWidget>
    with SingleTickerProviderStateMixin {
  bool isReaded = false;
  late AnimationController animCtrl;
  late Animation<Offset> tween;
  late Offset shakeOffset;

  @override
  void initState() {
    shakeOffset = Offset.zero;
    animCtrl = AnimationController(
        vsync: this, duration: const Duration(milliseconds: 150))
      ..addListener(() {
        setState(() {
          shakeOffset = tween.value;
        });
        if (animCtrl.isCompleted) {
          animCtrl.reverse();
        }
      });
    tween = Tween<Offset>(
      begin: const Offset(-8, -5),
      end: const Offset(5, 3),
    ).animate(CurvedAnimation(parent: animCtrl, curve: Curves.easeInOut));
    if (widget.actionAnimate != null) {
      widget.actionAnimate!(animCtrl);
    }
    // animCtrl.forward();
    super.initState();
  }

  @override
  void dispose() {
    animCtrl.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Transform.translate(
      offset: shakeOffset,
      child: InkWell(
        onTap: () {
          setState(() {
            isReaded = !isReaded;
          });
          widget.valueChanged(isReaded);
        },
        child: Container(
          padding: EdgeInsets.symmetric(vertical: 10.r),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              InkWell(
                onTap: () {
                  setState(() {
                    isReaded = !isReaded;
                  });
                  widget.valueChanged(isReaded);
                },
                child: Icon(
                  isReaded
                      ? Icons.check_box_rounded
                      : Icons.check_box_outline_blank,
                  color: isReaded ? ColorsUtil.blue : const Color(0xffdcdcdc),
                  size: 25.r,
                ),
              ),
              SizedBox(
                width: 5.r,
              ),
              RichText(
                text: TextSpan(
                  text: '请阅读并勾选',
                  style: TextStyle(
                    color: const Color(0xffdcdcdc),
                    fontSize: 14.sp,
                  ),
                  children: [
                    TextSpan(
                        text: '《用户协议》',
                        style: TextStyle(
                          color: ColorsUtil.blue,
                          fontSize: 14.sp,
                        ),
                        recognizer: TapGestureRecognizer()
                          ..onTap = () {
                            Get.toAgreeAssessment();
                          }),
                    TextSpan(
                      text: '和',
                      style: TextStyle(
                        color: const Color(0xffdcdcdc),
                        fontSize: 14.sp,
                      ),
                    ),
                    TextSpan(
                      text: '《隐私政策》',
                      style: TextStyle(
                        color: ColorsUtil.blue,
                        fontSize: 14.sp,
                      ),
                      recognizer: TapGestureRecognizer()
                        ..onTap = () {
                          Get.toAgreePrivacy();
                        },
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
